import {action, observable, computed} from 'mobx';
import {message} from 'antd';
import * as API from "../../apis/performance/reportListApi";
import {idsAndNamesToArr} from '../../util/Util'
import {accessAssessmentCycleApi} from "../../apis/performance/accessResultListApi";
import {WeaLocaleProvider} from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-考核报表
 * jiansong 2019/6/11
 */
export default class ReportListStore {

    @observable loading = true; //加载中
    @observable exportLoading = false; //导出加载中
    @observable data = []; //表格数据
    @observable total = 0; //数据数量
    @observable showSearchAd = false; // 高级搜索面板显示
    @observable yearDatas = []; //年份数组
    @observable stabList = []; //是否启用考核周期列表
    @observable msg = "";//返回错误信息

    @computed get hrmData() { //分部回显数据
        return idsAndNamesToArr(this.queryParams.hrmids, this.queryParams.hrmnames);
    }

    @computed get subcompanyData() { //分部回显数据
        return idsAndNamesToArr(this.queryParams.subcompanyids, this.queryParams.subcompanynames);
    }

    @computed get departmentData() { //部门回显数据
        return idsAndNamesToArr(this.queryParams.departmentids, this.queryParams.departmentnames);
    }

    @observable includesubData = [
        {key: "3", showname: getLabel('508780', '含子分部')},
        {key: "1", showname: getLabel('18919', '仅本分部')},
        {key: "2", showname: getLabel('18920', '仅子分部')},
    ];
    @observable includedeptData = [
        {key: "3", showname: getLabel('508784', '含子部门')},
        {key: "1", showname: getLabel('18916', '仅本部门')},
        {key: "2", showname: getLabel('18917', '仅子部门')},
    ];

    @observable month = [
        getLabel(25825, '1月'),
        getLabel(25826, '2月'),
        getLabel(25827, '3月'),
        getLabel(25828, '4月'),
        getLabel(25829, '5月'),
        getLabel(25830, '6月'),
        getLabel(25831, '7月'),
        getLabel(25832, '8月'),
        getLabel(25833, '9月'),
        getLabel(25834, '10月'),
        getLabel(25835, '11月'),
        getLabel(25836, '12月'),
    ]

    @observable quarter = [
        getLabel(509092, '1季度'),
        getLabel(509093, '2季度'),
        getLabel(509094, '3季度'),
        getLabel(509095, '4季度')
    ]

    @computed
    get halfyear() {
        const {year, year2} = this.queryParams
        let arr = []
        if (year != "") {
            arr.push(year + getLabel(20729, '半年'))
        }
        if (year2 != "") {
            let yearInt = parseInt(year)
            let year2Int = parseInt(year2)
            if (yearInt > year2Int) {
                for (let i = yearInt - 1; i >= year2Int; i--) {
                    arr.push(i + getLabel(20729, '半年'))
                }
            } else {
                for (let i = yearInt + 1; i <= year2Int; i++) {
                    arr.push(i + getLabel(20729, '半年'))
                }
            }
        }
        return arr
    }

    @computed
    get fullyear() {
        const {year, year2} = this.queryParams
        let arr = []
        if (year != "") {
            arr.push(year + getLabel(17138, '年度'))
        }
        if (year2 != "") {
            let yearInt = parseInt(year)
            let year2Int = parseInt(year2)
            if (yearInt > year2Int) {
                for (let i = yearInt - 1; i >= year2Int; i--) {
                    arr.push(i + getLabel(17137, '年度'))
                }
            } else {
                for (let i = yearInt + 1; i <= year2Int; i++) {
                    arr.push(i + getLabel(17138, '年度'))
                }
            }
        }
        return arr
    }

    @observable queryParams = {  //查询条件
        //查询条件
        type1: "1", //周期类型
        year: "",//年份
        year2: "", // 截止年份
        hrmids: null, //人员
        hrmnames: null, //人员名称
        includesub: "3", //是否包含分部，默认含子分部
        subcompanyids: null, //分部
        subcompanynames: null, //分部名称
        includedept: "3", //是否包含部门，默认含子部门
        departmentids: null, //部门
        departmentnames: null, //部门名称

        //分页参数
        pageSize: 10, //每页显示数量
        current: 1, //当前页数
    };

    @action("初始化数据")
    doInit = () => {
        //获取当前年份
        let date = new Date();
        let fullYear = date.getFullYear();
        let ydatas = [];
        for (let i = parseInt(fullYear) + 1; i > 2012; i--) {
            ydatas.push({key: i, showname: i});
        }
        //默认选中当前年份
        this.yearDatas = ydatas;
        this.queryParams = {  //查询条件
            //查询条件
            type1: "1", //周期类型
            year: fullYear.toString(),//年份
            year2: fullYear.toString(), // 截止年份
            hrmids: null, //人员
            hrmnames: null, //人员名称
            includesub: "3", //是否包含分部，默认含子分部
            subcompanyids: null, //分部
            subcompanynames: null, //分部名称
            includedept: "3", //是否包含部门，默认含子部门
            departmentids: null, //部门
            departmentnames: null, //部门名称

            //分页参数
            pageSize: 10, //每页显示数量
            current: 1, //当前页数
        };
        this.getaccessAssessmentCycleData();
    }

    @action("考核成绩是否启用考核周期")
    getaccessAssessmentCycleData = () => {
        this.loading = true;
        accessAssessmentCycleApi().then(action(res => {
            if (res.api_status) {
                this.stabList = res.stabList;
                this.topTab = res.stabList;
                if (res.ismonth) {
                    this.queryParams.type1 = "1";
                } else if (res.isquarter) {
                    this.queryParams.type1 = "2";
                } else if (res.ishyear) {
                    this.queryParams.type1 = "3";
                } else if (res.isfyear) {
                    this.queryParams.type1 = "4";
                }
                this.getReportListData();
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }))
    };

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("切换高级搜索框")
    setShowSearchAd = bool => this.showSearchAd = bool;

    @action("重置搜索条件")
    resetParams = () => {
        this.queryParams.hrmids = null;
        this.queryParams.hrmnames = null;
        this.queryParams.includesub = "3";
        this.queryParams.subcompanyids = null;
        this.queryParams.subcompanynames = null;
        ;
        this.queryParams.includedept = "3";
        this.queryParams.departmentids = null;
        this.queryParams.departmentnames = null;
    }

    @action("搜索")
    advanceSearch = () => {
        this.queryParams.current = 1;
        this.getReportListData();
        this.showSearchAd = false;
    };

    @action("绩效-考核报表-列表")
    getReportListData = () => {
        this.loading = true;
        API.reportListApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                this.data = res.data;
                this.total = res.total;
                if (this.stabList.length > 0) {
                    this.msg = "1";
                } else {
                    this.msg = "0";
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }))
    };

    @action("绩效-考核报表-导出")
    exportData = () => {
        this.exportLoading = true;
        API.reportExportApi(this.queryParams).then(action((data) => {
            if (data.url) {
                window.location.href = `${location.origin}${data.url}`
            }
            this.exportLoading = false;
        }))
    }

    @observable
    topTab = []

    @computed
    get columns() {
        let columns = [
            {
                title: getLabel('30042', '人员'),
                dataIndex: 'lastname',
                width: '120px',
                render: (text, record) => {
                    return <a href={'javaScript:openhrm(' + record.id + ');'}
                              onClick={event => window.pointerXY(event)}>{text}</a>
                }
            },
            {
                title: getLabel('33553', '分部'),
                dataIndex: 'subCompanyName',
                width: '120px'
            },
            {
                title: getLabel('27511', '部门'),
                dataIndex: 'departmentName',
                width: '120px'
            }
        ]
        switch (this.queryParams.type1) {
            case "1": {
                for (let index = 1; index < 13; index++) {
                    columns.push({
                        title: this.month[index - 1], dataIndex: "result" + index, width: '50px',
                        render: (text, record) => {
                            if (text == '') {
                                return <a href='javascript:void(0)'
                                          onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/accessView?resourceid=" + record.id + "&year=" + this.queryParams.year + "&type1=1&type2=" + index)}>-</a>
                            } else {
                                return <a href='javascript:void(0)'
                                          onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/accessView?scoreid=" + record["scoreid" + index])}>{text}</a>
                            }
                        }
                    })
                }
                break
            }
            case "2": {
                for (let index = 1; index < 5; index++) {
                    columns.push({
                        title: this.quarter[index - 1], dataIndex: "result" + index, width: '50px',
                        render: (text, record) => {
                            if (text == '') {
                                return <a href='javascript:void(0)'
                                          onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/accessView?resourceid=" + record.id + "&year=" + this.queryParams.year + "&type1=2&type2=" + index)}>-</a>
                            } else {
                                return <a href='javascript:void(0)'
                                          onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/accessView?scoreid=" + record["scoreid" + index])}>{text}</a>
                            }
                        }
                    })
                }
                break
            }
            case "3": {
                for (let index = 1; index <= this.halfyear.length; index++) {
                    columns.push({
                        title: this.halfyear[index - 1], dataIndex: "result" + index, width: '50px',
                        render: (text, record) => {
                            let tyear = parseInt(this.queryParams.year)
                            if (this.queryParams.year2) {
                                if (parseInt(tyear) > parseInt(this.queryParams.year2)) {
                                    tyear -= index - 1
                                } else {
                                    tyear += index - 1
                                }
                            }
                            if (text == '') {
                                return <a href='javascript:void(0)'
                                          onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/accessView?resourceid=" + record.id + "&year=" + tyear + "&type1=3")}>-</a>
                            } else if (text == "*") {
                                return <span>{text}</span>
                            } else {
                                return <a href='javascript:void(0)'
                                          onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/accessView?scoreid=" + record["scoreid" + index])}>{text}</a>
                            }
                        }
                    })
                }
                break
            }
            case "4": {
                for (let index = 1; index <= this.fullyear.length; index++) {
                    columns.push({
                        title: this.fullyear[index - 1], dataIndex: "result" + index, width: '50px',
                        render: (text, record) => {
                            let tyear = parseInt(this.queryParams.year)
                            if (this.queryParams.year2) {
                                if (parseInt(tyear) > parseInt(this.queryParams.year2)) {
                                    tyear -= index - 1
                                } else {
                                    tyear += index - 1
                                }
                            }
                            if (text == '') {
                                return <a href='javascript:void(0)'
                                          onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/accessView?resourceid=" + record.id + "&year=" + tyear + "&type1=4")}>-</a>
                            } else if (text == "*") {
                                return <span>{text}</span>
                            } else {
                                return <a href='javascript:void(0)'
                                          onClick={() => window.open("/spa/workrelate/static/index.html#/main/workrelate/performance/accessView?scoreid=" + record["scoreid" + index])}>{text}</a>
                            }
                        }
                    })
                }
                break
            }
        }
        return columns
    }
}